﻿using Microsoft.EntityFrameworkCore;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MemorySiteServer.Models
{
    public class DataContext : DbContext
    {
        //Add-Migration
        //Drop-Database
        //Remove-Migration
        //Update-Database
        public DataContext(DbContextOptions<DataContext> options) : base(options)
        { }
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<RolePermission> RolePermissions { get; set; }
        public DbSet<UserAudience> UserAudiences { get; set; }
        public DbSet<FavoriteGroup> FavoriteGroups { get; set; }
        public DbSet<Favorite> Favorites { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<FavoriteGroup>()
                .HasOne(e => e.Parent)
                .WithMany(e => e.Children)
                .OnDelete(DeleteBehavior.Cascade);
        }
    }

    public static class DbInitializer
    {
        public static void Initialize(DataContext context)
        {
            if (context.Users.Any())
            {
                return;
            }
            IdWorker idWoker = new IdWorker(1, 1);
            var user = new User()
            {
                ID = idWoker.NextId(),
                Name = "admin",
                Password = Util.MD5Hash("123456"),
                Role = new Role()
                {
                    ID = idWoker.NextId(),
                    Name = "管理员",
                    Level = 0,
                    RolePermissions = new List<RolePermission>()
                    {
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ROLE/GETPERMISSIONLIST",
                        },
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ROLE/CREATE",
                        },
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ROLE/GETLIST",
                        },
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ROLE/MODIFY",
                        },
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ROLE/GETONE",
                        },
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ROLE/DELETE",
                        },
                        new RolePermission()
                        {
                            ID = idWoker.NextId(),
                            Action = "/API/ACCOUNT/GETLIST"
                        }
                    }
                }
            };
            context.Users.Add(user);
            var role = new Role()
            {
                ID = idWoker.NextId(),
                Name = "游客",
                Level = 0,
                RolePermissions = null
            };
            context.Roles.Add(role);
            var favoriteGroup = new FavoriteGroup()
            {
                ID = idWoker.NextId(),
                Name = "收藏夹",
                ParentID = null
            };
            context.FavoriteGroups.Add(favoriteGroup);
            context.SaveChanges();
        }
    }
}
